import 'dart:io';

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_exit_app/flutter_exit_app.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:geomancy/enums/index.dart';
import 'package:geomancy/providers/system_provider.dart';
import 'package:geomancy/styles/index.dart';
import 'package:geomancy/tools/constant.dart';
import 'package:geomancy/tools/localstorage.dart';
import 'package:geomancy/widgets/common_text.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';

class FirstInstallAppPage extends StatefulWidget {
  const FirstInstallAppPage({super.key});

  @override
  State<FirstInstallAppPage> createState() => _FirstInstallAppPageState();
}

class _FirstInstallAppPageState extends State<FirstInstallAppPage> {
  bool showTipsDialog = false;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      FlutterNativeSplash.remove();
    });
  }

  void okConfirmHandle() async {
    context.read<SystemProvider>().setIsFirstInstallApp = FirstInstallApp.no;
    LocalStorage prefs = await LocalStorage.getInstance();
    prefs.setString(Constant.FIRSTINSTALLAPP, firstInstallAppValues[FirstInstallApp.no]!);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ColorStyle.pageDefaultColor,
      body: SafeArea(
        child: Stack(
          children: [
            Center(
              child: !showTipsDialog ? Container(
                width: 550.w,
                height: 700.w,
                padding: EdgeInsets.only(top: 40.w, left: 36.w, right: 36.w, bottom: 12.w),
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.all(Radius.circular(24.w))
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                        width: double.infinity,
                        margin: EdgeInsets.only(bottom: 12.w),
                        child: Text('个人信息保护提示', textAlign: TextAlign.center, style: TextStyle(fontSize: 30.sp, color: Colors.black, fontWeight: FontWeight.bold))
                    ),
                    Expanded(child: ListView(
                      padding: EdgeInsets.zero,
                      children: [
                        Text('欢迎来到择吉！', style: TextStyle(fontSize: 24.sp, height: 1.6, color: Colors.black87)),
                        RichText(text: TextSpan(
                            style: TextStyle(color: Colors.black87, fontSize: 24.sp, height: 1.6),
                            children: [
                              const TextSpan(text: '我们将通过'),
                              TextSpan(
                                text: '《用户协议》',
                                style: TextStyle(color: ColorStyle.red),
                                recognizer: TapGestureRecognizer()..onTap = () {
                                  context.push('/web_view', extra: {
                                    'url': 'https://fengshui.romko.top/home/user-agreement.html'
                                  });
                                },
                              ),
                              const TextSpan(text: '及'),
                              TextSpan(
                                text: '《隐私政策》',
                                style: TextStyle(color: ColorStyle.red),
                                recognizer: TapGestureRecognizer()..onTap = () {
                                  context.push('/web_view', extra: {
                                    'url': 'https://fengshui.romko.top/home/private-agreement.html'
                                  });
                                },
                              ),
                              const TextSpan(text: '帮助您了解我们为您提供的服务、我们如何处理个人信息以及您享有的权利。我们会严格按照相关法律法规要求，采取各种安全措施来保护您的个人信息。'),
                            ]
                        )),
                        Text('点击"同意"按钮，表示您已知情并同意以上协议和以下约定。', style: TextStyle(fontSize: 24.sp, height: 1.6, color: Colors.black87)),
                        Text('1.为了保障软件的安全运行和账户安全，我们会申请收集您的设备信息、IP地址、MAC地址。', style: TextStyle(fontSize: 24.sp, height: 1.6, color: Colors.black87)),
                        Text('2.上传或拍摄图片、视频，需要使用您的存储、相机、麦克风权限。', style: TextStyle(fontSize: 24.sp, height: 1.6, color: Colors.black87)),
                        Text('3.我们可能会申请位置权限，用于为您推荐您可能感兴趣的内容。', style: TextStyle(fontSize: 24.sp, height: 1.6, color: Colors.black87)),
                        Text('4.我们尊重您的选择权，您可以访问、修改、删除您的个人信息并管理您的授权，我们也为您提供注销、投诉渠道。', style: TextStyle(fontSize: 24.sp, height: 1.6, color: Colors.black87)),
                      ],
                    )),
                    SizedBox(height: 16.w,),
                    GestureDetector(
                      onTap: okConfirmHandle,
                      child: Container(
                        width: double.infinity,
                        height: 72.w,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                          color: ColorStyle.red,
                          borderRadius: BorderRadius.all(Radius.circular(36.w)),
                        ),
                        child: Text('同意', style: TextStyle(fontSize: 24.sp, color: Colors.white, fontWeight: FontWeight.bold),),
                      ),
                    ),
                    GestureDetector(
                      onTap: () {
                        setState(() {
                          showTipsDialog = true;
                        });
                      },
                      child: Container(
                        width: double.infinity,
                        height: 72.w,
                        alignment: Alignment.center,
                        color: Colors.transparent,
                        child: Text('不同意', style: TextStyle(fontSize: 24.sp, color: Colors.black87),),
                      ),
                    )
                  ],
                ),
              ) : Container(
                width: 550.w,
                height: 560.w,
                padding: EdgeInsets.only(top: 40.w, left: 36.w, right: 36.w, bottom: 12.w),
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.all(Radius.circular(24.w))
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                        width: double.infinity,
                        margin: EdgeInsets.only(bottom: 12.w),
                        child: Text('温馨提示', textAlign: TextAlign.center, style: TextStyle(fontSize: 30.sp, color: Colors.black, fontWeight: FontWeight.bold))
                    ),
                    Expanded(child: RichText(text: TextSpan(
                        style: TextStyle(color: Colors.black87, fontSize: 24.sp, height: 1.6),
                        children: [
                          const TextSpan(text: '如果您不同意'),
                          TextSpan(
                            text: '《用户协议》',
                            style: TextStyle(color: ColorStyle.red),
                            recognizer: TapGestureRecognizer()..onTap = () {
                              // context.push('/web_view', extra: {
                              //   'url': 'https://romko.top/user-agreement.html'
                              // });
                            },
                          ),
                          const TextSpan(text: '及'),
                          TextSpan(
                            text: '《隐私政策》',
                            style: TextStyle(color: ColorStyle.red),
                            recognizer: TapGestureRecognizer()..onTap = () {
                              // context.push('/web_view', extra: {
                              //   'url': 'https://romko.top/private-agreement.html'
                              // });
                            },
                          ),
                          const TextSpan(text: '很遗憾我们将无法为您提供服务。您需要同意以上协议后，才能使用择吉。我们将严格按照相关法律法规要求，坚决保障您的个人隐私和信息安全。'),
                        ]
                    ))),
                    SizedBox(height: 16.w,),
                    GestureDetector(
                      onTap: okConfirmHandle,
                      child: Container(
                        width: double.infinity,
                        height: 72.w,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                          color: ColorStyle.red,
                          borderRadius: BorderRadius.all(Radius.circular(36.w)),
                        ),
                        child: Text('同意并继续', style: TextStyle(fontSize: 24.sp, color: Colors.white, fontWeight: FontWeight.bold),),
                      ),
                    ),
                    GestureDetector(
                      onTap: () {
                        FlutterExitApp.exitApp(iosForceExit: true);
                      },
                      child: Container(
                        width: double.infinity,
                        height: 72.w,
                        alignment: Alignment.center,
                        child: Text('放弃使用', style: TextStyle(fontSize: 24.sp, color: Colors.black87),),
                      ),
                    )
                  ],
                ),
              ),
            ),
            Positioned(
              bottom: Platform.isIOS ? 0 : 24.w,
              left: 0,
              right: 0,
              child: SizedBox(
                width: double.infinity,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    ClipRRect(
                      borderRadius: BorderRadius.all(Radius.circular(20.w)),
                      child: Image.asset('assets/app_icon.png', width: 100.w),
                    ),
                    SizedBox(width: 20.w),
                    Column(
                      children: [
                        CommonText(txt: '每次堪舆，都有助提升运势', fontSize: 26.sp),
                        CommonText(txt: '© 吉有位 All rights reserved', fontSize: 20.sp)
                      ],
                    )
                  ],
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
